package cn.itcast.wanxinp2p.account.controller;


import cn.itcast.wanxinp2p.account.service.AccountService;
import cn.itcast.wanxinp2p.api.AccountAPI;
import cn.itcast.wanxinp2p.common.domain.RestResponse;
import cn.itcast.wanxinp2p.dto.account.AccountDTO;
import cn.itcast.wanxinp2p.dto.account.AccountLoginDTO;
import cn.itcast.wanxinp2p.dto.account.AccountRegisterDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 统一账户服务controller层
 * @author 六一居士
 */
@Slf4j
@Api(value = "统一账号服务", tags = "Account", description = "统一账号服务API")
@RestController
public class AccountController implements AccountAPI {

    /**
     * 注入service接口
     */
    @Autowired
    private AccountService accountService;


    /**
     * 获取手机验证码
     * @param mobile 手机号
     * @return 校验标识
     */
    @ApiOperation("获取手机验证码")
    @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String")
    @GetMapping("/sms/{mobile}")
    @Override
    public RestResponse getSMSCode(String mobile) {
        return accountService.getSMSCode(mobile);
    }


    /**
     * 校验手机号和验证码
     * @param mobile 手机号
     * @param key 校验标识
     * @param code 验证码
     * @return 1(成功) / 0(失败)
     */
    @ApiOperation("校验手机号和验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile",value = "手机号",required = true,dataType = "String"),
            @ApiImplicitParam(name = "key",value = "校验标识",required = true,dataType = "String"),
            @ApiImplicitParam(name = "code",value = "验证码",required = true,dataType = "String")
    })
    @GetMapping(value = "/mobiles/{mobile}/key/{key}/code/{code}")

    @Override
    public RestResponse<Integer> checkMobile(@PathVariable("mobile") String mobile,
                                             @PathVariable("key") String key,
                                             @PathVariable("code") String code) {
        return RestResponse.success(accountService.checkMobile(mobile, key, code));
    }


    /**
     * 统一账号服务实现保存功能
     * @param accountRegisterDTO 客户端用户传输的基础数据
     * @return 返回用户数据
     */
    @ApiOperation("用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountRegisterDTO",
                    value = "账户注册信息",
                    required = true,
                    dataType = "AccountRegisterDTO",
                    paramType = "body")
    })
    @PostMapping(value = "/l/accounts")
    @Override
    public RestResponse<AccountDTO> register(@RequestBody AccountRegisterDTO accountRegisterDTO) {
        return RestResponse.success(accountService.register(accountRegisterDTO));
    }


    @ApiOperation("用户登录")
    @ApiImplicitParam(name = "accountLoginDTO",value = "登录信息", required =true,dataType = "AccountLoginDTO",paramType= "body")
    @PostMapping(value = "/l/accounts/session")
    @Override
    public RestResponse<AccountDTO> login(@RequestBody AccountLoginDTO accountLoginDTO) {
        return RestResponse.success(accountService.login(accountLoginDTO));
    }


}
